myApp.controller('UserListController', function ($scope, $stateParams,ngDialog,$http) {
    $scope.userList = [];

    $scope.canUpdateUser = function () {
        return isUserCanOperateByRole('user_admin');
    };
    
    $http.get(python_url + '/api/v1/user/?withRole=true&token_id=' + getTokenId()).success(function (data) {
        $scope.userList = data.data;


        angular.forEach($scope.userList, function (list) {
            list.role_name = '';
            angular.forEach(list.role_list, function (role) {
                list.role_name += role.desc + ' ';

            })

        });
    });

    $scope.total=$scope.userList.length;

    $scope.page = {size: 15, index: 1};

    $scope.add = function () {

        window.location.href = "#/menu/userAdd/";
    };
    $scope.edit = function (id) {
        if (id == undefined) {
            id = '';
        }
        window.location.href = "#/menu/userUpdate/" + id;
    };
    $scope.del = function (id) {
         openDialog(ngDialog, $scope, "该用户删除不可恢复，请确认！?", true, function (val) {
            if (!val) {
                return;
            }

            $http.get(python_url + '/api/v1/user/del?token_id='+getTokenId()+'&id=' + id).success(function (data) {

                reload("#/menu/userList/");

            })
        })
    };
});





myApp.controller('UserAddController', function ($scope, $stateParams, $http) {


    $scope.info = {};
    $scope.info.account = "";
    $scope.info.password = "";
    $scope.info.realname = "";
    $scope.info.role = [];

    function createSubTree() {
        $scope.rolelist = {};
        $.ajax({
            type: "GET",
            url: python_url + '/api/v1/role/?token_id=' + getTokenId(),
            async: false,
            success: function (data) {
                $scope.rolelist = data.data;

            }
        });
    }

    createSubTree();

    $scope.save = function () {
        var obj = [];
        angular.forEach($scope.rolelist, function (data) {
            if (data.Selected == true) {
                obj.push(data.role_id);
            }

        });
        if ($scope.info.account == "") {
            alert("用户名不能为空!");
            return;
        }
        ;
        if ($scope.info.password == "") {
            alert("密码不能为空!");
            return;
        }
        if ($scope.info.realname == "") {
            alert("真实姓名不能为空!");
            return;

        }


        $http.get(python_url + '/api/v1/user/add?token_id='+getTokenId()+'&account=' + $scope.info.account + '&password=' + $scope.info.password + '&role=' + JSON.stringify(obj) + '&realname=' + $scope.info.realname).success(function (data) {
            if (data.success == true) {
                window.location.href = "#/menu/userList/";
            }
        })

    };
    $scope.goBack = function () {
        window.location.href = "#/menu/userList/";
    };
});
myApp.controller('PassUpdateController', function ($scope, $stateParams, $http) {
    var id = $stateParams.id;
    $scope.info = {};
    $scope.info.old_PassWord = "";
    $scope.info.new_PassWord = "";
    $scope.info.update_pass = false;

    function userlist() {

        $.ajax({
            type: "GET",
            url: python_url + '/api/v1/user/?withRole=true&id=' + id+'&token_id='+getTokenId(),
            async: false,
            success: function (data) {
                $scope.info = data.data[0];
            }
        });
    }

    userlist();

    function createSubTree() {
        $scope.rolelist = {};
        $.ajax({
            type: "GET",
            url: python_url + '/api/v1/role/?token_id=' + getTokenId(),
            async: false,
            success: function (data) {
                $scope.rolelist = data.data;

            }
        });
    }

    createSubTree();


    angular.forEach($scope.info.role_list, function (j) {
        var id = j.role_id;
        angular.forEach($scope.rolelist, function (i) {
            if (id == i.role_id) {
                i.Selected = true;
            }
        })
    })


    // $scope.runIng = false;

    $scope.save = function () {
        if ($scope.info.update_pass == undefined) {
            $scope.info.update_pass = false;
        }
        if ($scope.info.account == "") {
            alert("用户名不能为空!");
            return;
        }
        ;
        if ($scope.info.realname == "") {
            alert("真实姓名不能为空!");
            return;

        }

        var obj = [];
        angular.forEach($scope.rolelist, function (list) {
            if (list.Selected == true) {
                obj.push(list.role_id);
            }

        });
        if ($scope.info.update_pass == true) {
            if ($scope.info.old_PassWord == "") {
                alert("请输入旧密码!");
                return;
            }
            if ($scope.info.new_PassWord == "") {
                alert("请输入新密码!");
                return;
            }

            $http.get(python_url + '/api/v1/user/update?token_id='+getTokenId()+'&account=' + $scope.info.account + '&update_pass=' + $scope.info.update_pass + '&role_list=' + JSON.stringify(obj) + '&id=' + id + '&new_password=' + $scope.info.new_PassWord + '&old_password=' + $scope.info.old_PassWord + '&realname=' + $scope.info.realname).success(function (data) {
                if (data.success == true) {
                    window.location.href = "#/menu/userList/";
                } else {
                    alert(data.msg);
                }
            })

        } else {
            $http.get(python_url + '/api/v1/user/update?token_id='+getTokenId()+'&account=' + $scope.info.account + '&update_pass=' + $scope.info.update_pass + '&role_list=' + JSON.stringify(obj) + '&id=' + id + '&realname=' + $scope.info.realname).success(function (data) {
                if (data.success == true) {
                    window.location.href = "#/menu/userList/";
                } else {
                    alert(data.msg);
                }
            })
        }

    };
    $scope.goBack = function () {
        window.history.back();
    };
});